package com.example.sql.manager.controller;

import com.example.sql.manager.annotations.AddTraceId;
import com.example.sql.manager.common.ResponseEntity;
import com.example.sql.manager.constants.Url;
import com.example.sql.manager.entity.po.FuncPO;
import com.example.sql.manager.entity.vo.FuncVO;
import com.example.sql.manager.service.FuncService;
import com.example.sql.manager.utils.UpdateTools;
import com.example.sql.manager.validator.FuncValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "FuncController", tags = {"Func 的基础接口"})
@Controller
@RequestMapping(Url.FUNC)
public class FuncController {


    @Resource
    private FuncService funcService;

    @ApiOperation(value = "新增修改一个 Func ")
    @AddTraceId
    @ResponseBody
    @PostMapping
    public Object saveFunc(@RequestBody FuncPO func) {
        try {
            FuncValidator.validate(func);
            Long id = this.funcService.saveFunc(func);
            return ResponseEntity.success(id.toString());
        } catch (Exception e) {
            return ResponseEntity.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "查询 Func 详情")
    @AddTraceId
    @ResponseBody
    @GetMapping(value = "/{id:\\d+}")
    public Object getFunc(@PathVariable Long id) {
        try {
            FuncPO func = this.funcService.getById(id);
            FuncVO funcVO = new FuncVO();
            UpdateTools.mergeProperties(func, funcVO);
            return ResponseEntity.success(funcVO);
        } catch (Exception e) {
            return ResponseEntity.failure(e.getMessage());
        }
    }
}
